<template>
	<xn-form-container
		:title="formData.id ? '编辑' : '增加'"
		:width="550"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" :label-col="{ style: { width: '180px' } }">
			<a-form-item label="规则名称：" name="name">
				<a-input
					v-model:value="formData.name"
					:disabled="formData.defaultFlag === CommonDetermineEnum.YES"
					placeholder="请输入规则名称"
					allow-clear
					style="width: 300px"
				/>
			</a-form-item>
			<a-form-item label="适用范围：" name="suitRange">
				<a-tree-select
					v-model:value="formData.suitRange"
					style="width: 300px"
					:tree-data="orgUserTreeData"
					tree-checkable
					allow-clear
					:show-checked-strategy="SHOW_PARENT"
					placeholder="请选择适用范围"
					tree-node-filter-prop="label"
					:fieldNames="fieldNames"
					max-tag-count="responsive"
					:dropdownMatchSelectWidth="false"
					treeNodeFilterProp="name"
				/>
			</a-form-item>
			<a-form-item label="拥有最大客户数：" name="max">
				<a-input-number
					v-model:value="formData.max"
					:min="1"
					placeholder="请输入拥有最大客户数"
					allow-clear
					style="width: 300px"
				/>
			</a-form-item>
			<a-form-item label="成单客户是否占拥有客户数：" name="occupyFlag">
				<a-radio-group v-model:value="formData.occupyFlag" :options="occupyFlagOptions" />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="customerRetentionForm">
	import { required } from '@/utils/formRules'
	import tool from '@/utils/tool'
	import { CommonDetermineEnum } from '@/utils/enum'
	import { TreeSelect } from 'ant-design-vue'
	import customerRetentionApi from '@/api/biz/crm/customerRetentionApi'

	// 定义emit事件
	const emit = defineEmits({ successful: null })
	// 默认是关闭状态
	const visible = ref(false)
	const formRef = ref()
	// 表单数据，也就是默认给一些数据
	const formData = ref({
		suitRange: [],
		occupyFlag: CommonDetermineEnum.NO
	})
	// 定义机构元素
	const submitLoading = ref(false)

	const SHOW_PARENT = TreeSelect.SHOW_PARENT
	const fieldNames = {
		label: 'name',
		value: 'id'
	}

	const orgUserTreeData = ref([])
	const occupyFlagOptions = tool.dictList('COMMON_DETERMINE')

	const orgUserTree = (id) => {
		customerRetentionApi.orgUserTree({ id }).then((data) => {
			orgUserTreeData.value = data
		})
	}

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true

		orgUserTree(record ? record.id : null)

		if (record) {
			formData.value = Object.assign({}, { ...record, suitRange: record.suitRange.split(',') })
		}
	}
	// 关闭抽屉
	const onClose = () => {
		visible.value = false
		formData.value = {
			suitRange: [],
			occupyFlag: CommonDetermineEnum.NO
		}
	}
	// 默认要校验的
	const formRules = {
		name: [required('请输入客户来源名称')],
		suitRange: [required('请选择适用范围')],
		max: [required('请输入拥有最大客户数')]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			customerRetentionApi
				.submitForm({ ...formData.value, suitRange: formData.value.suitRange.join(',') }, formData.value.id)
				.then(() => {
					visible.value = false
					formRef.value.resetFields()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
